package com.huifai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huifai.common.constant.SysConstant;
import com.huifai.entity.SysRole;
import com.huifai.entity.SysUser;
import com.huifai.entity.SysUserRole;
import com.huifai.mapper.SysRoleMapper;
import com.huifai.mapper.SysUserRoleMapper;
import com.huifai.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huifai.vo.request.SysRoleRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wcj
 * @since 2019-07-16
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysUserRoleMapper   sysUserRoleMapper;
    @Autowired
    private SysRoleMapper   sysRoleMapper;
    @Override
    public List<SysRole> findRolesByUserId(Long userId) {
        Map<String,Object>  paramMap=new HashMap<>();
        paramMap.put("user_id",userId);
        List<SysUserRole> sysUserRoleList=sysUserRoleMapper.selectByMap(paramMap);
        if(sysUserRoleList!=null&&sysUserRoleList.size()>0){
            List<Long> roleIdList=new ArrayList<>();
            for (SysUserRole sysUserRole : sysUserRoleList) {
                roleIdList.add(sysUserRole.getRoleId());
            }
            QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
            queryWrapper.in("id",roleIdList);
            return sysRoleMapper.selectList(queryWrapper);
        }
        return null;
    }

    @Override
    public List<SysRole> findRolesByRoleName(String roleName) {
        QueryWrapper<SysRole>  queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_name",roleName);
        return sysRoleMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<SysRole> pageQuery(SysRoleRequest sysRoleRequest, Page<SysRole> page) {
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(sysRoleRequest.getRoleName())){
            queryWrapper.like("role_name",sysRoleRequest.getRoleName());
        }
        queryWrapper.eq("status", SysConstant.DataStatus.STATUS_NORMAL);
        queryWrapper.orderByDesc("create_time");
        return sysRoleMapper.selectPage(page,queryWrapper);
    }
}
